package com.fudan.sms;


import com.fudan.sms.dto.ProductEntity;
import com.fudan.sms.enums.ShopOperateEnum;
import com.fudan.sms.mapper.ProductMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;

public class Main {
    static Scanner input = new Scanner(System.in);
    private static SqlSessionFactory factory;
    private static InputStream inputStream;

    static {
        try {
            inputStream = Resources.getResourceAsStream("mybatis-config.xml");
            factory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static String menu = new StringBuffer()
            .append("********请选择菜单*********").append("\n")
            .append("*    1.输出所有商品     *").append("\n")
            .append("*    2.增加一种商品     *").append("\n")
            .append("*    3.删除一种商品     *").append("\n")
            .append("*    4.查询一种商品     *").append("\n")
            .append("*    5.退出系统         *").append("\n")
            .append("*****************************").toString();

    static String showTitle = new StringBuffer()
            .append("商品编号").append("\t")
            .append("商品名称").append("\t")
            .append("商品价格").append("\t").toString();

    public static void main(String[] args) {
        System.out.println("欢迎您进入商品管理页面！");
        while (true) {
            System.out.println(menu);
            System.out.println("请选择：");
            String next = input.next();
            ShopOperateEnum shopOperateEnum = ShopOperateEnum.getEnum(next);
            switch (shopOperateEnum) {
                case PRINT:
                    printAll();
                    break;
                case ADD:
                    addProduct();
                    break;
                case DELETE:
                    deleteProduct();
                    break;
                case SELECT:
                    getProduct();
                    break;
                case QUIT:
                    System.out.println("正在退出....");
                    return;
                default:
                    System.out.println("输入错误！请根据菜单重新选择！");
                    System.out.println(menu);
                    break;
            }
        }
    }


    /**
     * 查詢交互
     */
    private static void getProduct() {
        System.out.println("请输入查询商品的编号:");
        Integer id = Integer.valueOf(input.next());
        ProductEntity productEntity = get(id);
        if (Objects.isNull(productEntity)) {
            System.out.println("无法找到此商品!");
        } else {
            System.out.println(showTitle);
            System.out.println(productEntity);
        }
    }

    private static ProductEntity get(Integer id) {
        SqlSession session = null;
        ProductEntity productEntity = null;
        try {
            session = factory.openSession();
            ProductMapper mapper = session.getMapper(ProductMapper.class);
            productEntity = mapper.get(id);
            session.commit();
        } finally {
            session.close();
        }
        return productEntity;
    }


    /**
     * 刪除交互
     */
    private static void deleteProduct() {
        System.out.println("请输入被删除商品的编号:");
        Integer id = Integer.valueOf(input.next());
        int counter = delete(id);
        if (counter > 0) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除数据不存在！");
        }
    }

    private static int delete(Integer id) {
        SqlSession session = null;
        int count = 0;
        try {
            session = factory.openSession();
            ProductMapper mapper = session.getMapper(ProductMapper.class);
            count = mapper.delete(id);
            session.commit();
        } finally {
            session.close();
        }
        return count;
    }


    /**
     * 插入交互
     */
    private static void addProduct() {
        ProductEntity productEntity = null;
        Integer id = null;
        do{
            System.out.println("请输入商品编号:");
             id = Integer.valueOf(input.next());
            productEntity = get(id);
            if (Objects.nonNull(productEntity)){
                System.out.println("该商品编号已被占用,请重新输入！");
            }
        }while (Objects.nonNull(productEntity));



        System.out.println("请输入商品名：");
        String pname = input.next();
        System.out.println("请输入品牌：");
        String brand = input.next();
        System.out.println("请输入价格：");
        Float prcie = Float.valueOf(input.next());
        System.out.println("请输入库存：");
        Integer stock = Integer.valueOf(input.next());

        ProductEntity saveEntity = ProductEntity.builder().id(id)
                .pname(pname)
                .brand(brand)
                .prcie(prcie)
                .stock(stock).build();
        save(saveEntity);
        System.out.println("添加成功");
    }

    private static int save(ProductEntity entity) {
        SqlSession session = null;
        int count = 0;
        try {
            session = factory.openSession();
            ProductMapper mapper = session.getMapper(ProductMapper.class);
            count = mapper.save(entity);
            session.commit();
        } finally {
            session.close();
        }
        return count;
    }


    /**
     * 打印所有商品
     */
    private static void printAll() {
        List<ProductEntity> productEntities = getAll();
        System.out.println(showTitle);
        //已重写ProductEntity toString()
        productEntities.forEach(System.out::println);
        System.out.println("");
    }

    private static List<ProductEntity> getAll() {
        SqlSession session = null;
        List<ProductEntity> list = new ArrayList<>();
        try {
            session = factory.openSession();
            ProductMapper mapper = session.getMapper(ProductMapper.class);
            list = mapper.getAll();
            session.commit();
        } finally {
            session.close();
        }
        return list;
    }


}
